<?php
require_once('functions/pageLoad.php');
loginRequired();

$user_session = explode("_", $_SESSION['user']);
$user_id = $user_session[0];
$page_title = 'My subscription';

// current subscription details

if($_GET['cancel'] == 'true'){

	$sql = "SELECT id, paypal_profile_id FROM tradesman WHERE user_id = '".$user_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rs=mysql_fetch_assoc($query);
	
	$tradesman_id = $rs['id'];

	if(strlen($rs['paypal_profile_id']) > 0){
	
	include "class.paypal.recurring.php";
	$obj=new paypal_recurring;

	$obj->environment = 'live';	// or 'beta-sandbox' or 'live'
	$obj->paymentType = urlencode('Authorization');				// or 'Sale' or 'Order'

	// Set request-specific fields.
	$obj->startDate = urlencode(date("c", strtotime($expires)));
	$obj->billingPeriod = urlencode("Year");				// or "Day", "Week", "SemiMonth", "Year"
	$obj->billingFreq = urlencode("1");		// combination of this and billingPeriod must be at most a year
	$obj->currencyID = urlencode('GBP');			// or other currency code ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')

	/* PAYPAL API  DETAILS */
	$obj->API_UserName = urlencode($settings['paypal_api_username']);
	$obj->API_Password = urlencode($settings['paypal_api_password']);
	$obj->API_Signature = urlencode($settings['paypal_api_signature']);
	$obj->API_Endpoint = "https://api-3t.paypal.com/nvp";

	/*SET SUCCESS AND FAIL URL*/
	$obj->returnURL = urlencode("http://".$settings['domain_name']."/my-subscription?complete=true");
	$obj->cancelURL = urlencode("http://".$settings['domain_name']."/my-subscription?error=true");
	
	
	$cancel_result = $obj->ManageRecurringPaymentsProfileStatus($rs['paypal_profile_id']);
	
	}

	// delete tradesman
	
	$sql = "DELETE FROM tradesman WHERE user_id = '".$user_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	$sql = "DELETE FROM users WHERE id = '".$user_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM visit_requests WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM lead_purchases WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM quotes WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM questions WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM messages WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM reviews WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM tradesman_to_area WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM tradesman_to_trade WHERE tradesman_id = '".$tradesman_id."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	unset($_SESSION['user']);
	header("Location: /cancelled");

}


include('includes/meta.php');
include('includes/header.php');
include('includes/navigation.php');

?>

<h1>Cancel subscription</h1>

<p>By clicking the confirmation button below, your account will be completely erased from our servers and any payment subscription you have with Paypal will be cancelled. This action is irreversable, if you require an account with us at a later time, you will need to register again.</p>

<a href="?cancel=true" class="blue_link" style="margin-right: 10px; font-size: 12px;">Yes, please completely remove my account.</a> <a href="/my-subscription" class="blue_link" style="font-size: 12px;">No thanks, i'll keep my account</a>


<?php 

include('includes/rightColumn.php');
include('includes/footer.php');

?>